﻿<link rel="stylesheet" href="~/lib/layui/css/layui.css">

<div class="layui-fluid" style="margin-top:20px;margin-bottom:50px;padding:10px;" id="noticeList">
</div>


<script src="~/lib/layui/layui.js"></script>
@if (CheckExt.AuthView(ViewBag.sr, "notice_read"))
{
    <script>var auth = 1;</script>
}else{
     <script>var auth = 0;</script>
}
<script>
 layui.use(['element', 'layer', 'jquery','util'], function () {
    var element = layui.element, layer = layui.layer, $ = layui.jquery,util = layui.util;
    $.getJSON("/Admin/Notice/Index?json=1&readList=1", function (res) {
        if(res.data.length == 0){
             $('#noticeList').append('<div class="layui-card" ><div class="layui-card-header" style="overflow:hidden;"><h3>没有新通知！</h3></div></div>');
        }else{
            if(auth == 1){
                $.each(res.data, function (index, value) {
                      $('#noticeList').append('<div class="layui-card" style="background-color:#f7fcfe"><div class="layui-card-header" style="overflow:hidden;"><h3>'
                                    + value.title + '</h3></div><div class="layui-card-body" ><p style="word-wrap:break-word">'
                                    +  Substr(value.content,0,40) + '<button type="button" class="layui-btn layui-btn-xs" onclick="read('+value.id+','+ value.noticeId + ')">详细</button></p><span style="font-size:12px;color:green;">'
                                    + util.timeAgo(value.postTime) +'</span></div></div>');
                 });
            }else{
                 $.each(res.data, function (index, value) {
                      $('#noticeList').append('<div class="layui-card" style="background-color:#f7fcfe"><div class="layui-card-header" style="overflow:hidden;"><h3>'
                                    + value.title + '</h3></div><div class="layui-card-body" ><p style="word-wrap:break-word">'
                                    +  Substr(value.content,0,40) + '</p><span style="font-size:12px;color:green;">'
                                    + util.timeAgo(value.postTime) +'</span></div></div>');
                 });
            }
        }
    });
});

function read(id,noticeId){
    parent.layer.open({
         type: 2,
         title: "通知详情",
         area: ['800px', '500px'],
         closeBtn: 1, //不显示关闭按钮
         anim: 2,
         shadeClose: true, //开启遮罩关闭
         content:"/Admin/Notice/Read?id=" + id + "&noticeid=" + noticeId
     });
 }

 function Substr(str, start, n) { 
   if (str.replace(/[\u4e00-\u9fa5]/g, '**').length <= n) {
     return str;
   }
   let len = 0;
   let tmpStr = '';
   for (let i = start; i < str.length; i++) { // 遍历字符串
     if (/[\u4e00-\u9fa5]/.test(str[i])) { // 中文 长度为两字节
       len += 2;
     } else {
       len += 1;
     }
     if (len > n) {
       break;
     } else {
       tmpStr += str[i];
     }
   }
   return tmpStr + " ...";
 }

</script>